package com.senthink.gl.business.common.util;


import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 加密
 *
 * @author zyz
 * Date 2019/09/24 12:53
 * Copyright (C)
 */
public class EncryptUtils {

    //length用户要求产生字符串的长度
    public static String getRandomString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    public static String encrypt(String content,String password) {
        try {
            if (content.isEmpty())
                return "";
            // 使用密钥生成器
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(password.getBytes());
            kgen.init(128, secureRandom);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");// 创建密码器
            byte[] byteContent = content.getBytes("utf-8");
            cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
            byte[] result = cipher.doFinal(byteContent);
            String str = Base64.encodeBase64String(result);

            return str; // 加密
        } catch (Exception e) {
             e.printStackTrace();
        }
        return null;
    }


    public static String decrypt(String str,String password) throws Exception{
        byte[] content = Base64.decodeBase64(str);
        // 使用密钥生成器
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed(password.getBytes());
        kgen.init(128, secureRandom);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
        Cipher cipher = Cipher.getInstance("AES");// 创建密码器
        cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
        byte[] result = cipher.doFinal(content);
        return new String(result);
    }

    public static void main(String[] args) throws Exception {
//        String randomString = getRandomString(40);
//        String encrypt = encrypt("{\n" +
//                "\t\"order\": {\n" +
//                "\t\t\"topical\": \"呵呵呵呵\",\n" +
//                "\t\t\"timeLimit\": 8,\n" +
//                "\t\t\"urgency\": 1,\n" +
//                "\t\t\"remark\": null\n" +
//                "\t},\n" +
//                "\t\"fault\": [{\n" +
//                "\t\t\"faultDevice\": \"wangze\",\n" +
//                "\t\t\"deviceId\": \"18567126121\",\n" +
//                "\t\t\"faultInfo\": \"wangze\",\n" +
//                "\t\t\"type\": 2,\n" +
//                "\t\t\"address\": \"11.1422,141.153\",\n" +
//                "\t\t\"remark\": \"null\"\n" +
//                "\t}, {\n" +
//                "\t\t\"faultDevice\": \"zhaoyanze\",\n" +
//                "\t\t\"deviceId\": \"185671264121\",\n" +
//                "\t\t\"faultInfo\": \"zhaoyanze\",\n" +
//                "\t\t\"type\": 1,\n" +
//                "\t\t\"address\": \"11.4232,141.153\",\n" +
//                "\t\t\"remark\": \"null\"\n" +
//                "\t}],\n" +
//                "\t\"orderTriage\": {\n" +
//                "\t\t\"name\": \"11\",\n" +
//                "\t\t\"remark\": \"11\"\n" +
//                "\t}\n" +
//                "}", "31542lad3113adadsj");
//
//        System.out.println(encrypt);

        /*String str = encrypt("{\n" +
                "    \"newOrder\": {\n" +
                "        \"topical\": \"当前轮询共1个异常\",\n" +
                "        \"timeLimit\": 9,\n" +
                "        \"urgency\": 1,\n" +
                "        \"remark\": \"备注\"\n" +
                "    },\n" +
                "    \"fault\": [\n" +
                "        {\n" +
                "            \"faultDevice\": \"screen\",\n" +
                "            \"deviceId\": \"185da67fa126121\",\n" +
                "            \"faultInfo\": \"此类型共有1个异常\",\n" +
                "            \"type\": \"1\",\n" +
                "            \"address\": \"113.751896,34.850458\",\n" +
                "            \"remark\": null\n" +
                "        }\n" +
                "    ],\n" +
                "    \"orderTriage\": {\n" +
                "        \"name\": \"2\",\n" +
                "        \"remark\": \"2\"\n" +
                "    }\n" +
                "}","MNxajaFPryPDTrFXUWlTdrQNM6vRrRMR");
        System.out.println(str);*/

        /*String decrypt = decrypt("0CIHpEGP7hyvHesJG7V9DDP8F4iowQks61n5XH4xHfduFdoc7sXODuHJ822LOy4LKGol1ZH5+JUYmgJ0qrTJM550AD/AEh0JdPzbyhcRyu6VuWI/0SvPImKX7P8PFaVaIQtvxps/tG37sYnPGLppQI2ZtWJQNK92gTqJlcu3zorScWrwnOnjOKtCms8aUuSzPi3x53MwpRHUl1o+XQ7eHCJzL2qr52V9SRBPc2ghaamwVaNyu8s+Po+D/0DI9zVnFF3H0OXSF8vT/LPHhjBj3VlvMtb7A+GfDvs44DT8LM2Yx6rpXMUp+f1Uor/HGPA/yhZnPfXcwOdoTODT0X1eHwwo1HAfSmjfDeCPLLU6kTwdL6kgjAoH0aRc95ClZ9y/NLDD/gMgV1th8aEk/lGgrBNCIA2onRU2bifrtsYmrTnLjFpH0UnPknKL0ItRbwvku7n0xRBWgmFZaG+m+eTG8iDZnHMIwKqLkqP1l5m6kIcW7UJ5/njSIYw8Bhhfd+TdP9WFuO4jvwDwuvONbYlyDyibmX3i6WHn/4znvgdsmOfkd0yjg3eQ6O7jUx1B8nwIwHAS8reYJYDg795LV3ISP1Eq6OeGr7PYwb8QlSfhQ8DTw6TOXvLHEVBgf85kOdRTgjf/mP6GoHSuYf0MZddpFwx/Dr3+hum2ismLr/54JSsw9twgeVzeU4Et9LRMXXMf",
                "31542lad3113adadsj");
        System.out.println(decrypt);*/

        String decrypt = decrypt("VXXPQceLhUu5N4JYxTGTzpeOtggxznvd5c4qvYr22x2WnS0NE3e9Yah3ymLa6bKnY+OI8zxb+BqieOjy0TciHBAyGKw+nYEyIr64Sw7Gefr9dF9VJd7DZUOkMCWl0zeiQkuvCPwkvUlB+p+dZjC++gRDENfS8JW+iJX5p8UbUr+EftZFrpPQjcKpoNOK4bnByGJFDppGvbFsTtYqW01eLHOEubR6/lIU/BSFpR1h/ixFvAGrZx7he1c6rL1WqaycZ9hqDc9ytKpWRlHxtqD2wXESoHjB6c1pbpQI9bda9ABtBAdjyTQzTokpaJTTV0pY9gxiylP/ELYizeImAt4EJON2/GPjRWGqgB61pOEvpjBXkOhy+w5EwbqvjcGVUqvbZDRjM2Np1gInFO2nPeSltYf8d4TVZEVmpGea6knhi/RMyOQzYnHbxlsva5VLPm3u4fKmZ9p9hXZJIM87uryYHGujpOTMQkCR9NZCiPA4mWv9zTD8Ryl7D6N9lBv8Ff5ItBeaH0/emTrsjplF63VCnnzusbj0EXrbfVLOgbx8ijggpqEp5043OKRs+zSoHrz0qpT/T9A8HEKwD3s3EmALSuno3T8ua2Fge+wEaVuNw7uNcSJJ51bJMyDLtxePY9F1xn+JZ6D2Xdf5sv3S+14JVg==",
                "MNxajaFPryPDTrFXUWlTdrQNM6vRrRMR");
        System.out.println(decrypt);

        /*String d = decrypt("AbLWRRvr/Su5EeCnqzXiZQ==","MNxajaFPryPDTrFXUWlTdrQNM6vRrRMR");
        String[] split = d.split(",");
        List<Integer> a = new ArrayList();
        for (String s : split) {
            a.add(Integer.parseInt(s));
        }
        System.out.println(a);*/
    }
}
